package com.hkd.blogsystem.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.ICaptcha;
import com.hkd.blogsystem.mapper.BlogInfoMapper;
import com.hkd.blogsystem.mapper.UserInfoMapper;
import com.hkd.blogsystem.model.BlogInfo;
import com.hkd.blogsystem.model.CaptchaProperties;
import com.hkd.blogsystem.model.UserInfo;
import com.hkd.blogsystem.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo selectUserInfoByName(String userName) {
        return userInfoMapper.selectByName(userName);
    }

    public UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo selectAuthorInfoByBlogId(Integer blogId) {
        //1. 根据博客ID, 获取作者ID
        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
        //2. 根据作者ID, 获取作者信息
        if (blogInfo == null || blogInfo.getUserId()==null){
            return null;
        }
        UserInfo userInfo = userInfoMapper.selectById(blogInfo.getUserId());
        return userInfo;
    }

    public Integer registeerUserInfo(String username,String password) {
        String securityPass =  SecurityUtils.encrypt(password);
        return userInfoMapper.registerUser(username,securityPass);
    }
}
